package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhou.dao.CheckItemMapper;
import com.zhou.entity.PageResult;
import com.zhou.pojo.CheckItem;
import com.zhou.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 服务提供类
 */
@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;

    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.insert(checkItem);
    }

    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> checkItems = checkItemMapper.pageQuery(queryString);
        return new PageResult(checkItems.getTotal(), checkItems.getResult());
    }

    @Override
    public void deleteById(Integer id) throws RuntimeException {
        Long count = checkItemMapper.findCountByCheckItemId(id);
        if (count > 0) {
            //当前检查项被引用，不能删除
            throw new RuntimeException("当前检查项被引用，不能删除");
        }
        checkItemMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.updateByPrimaryKeySelective(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkItemMapper.findAll();
    }
}
